package com.ytjj.quartz.service;

import com.alibaba.nacos.common.utils.Md5Utils;
import com.gexin.fastjson.JSONObject;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.mall.mapper.AdvertReportMapper;
import com.ytjj.qmyx.mall.mapper.ProductMapper;
import com.ytjj.qmyx.mall.model.AdvertReport;
import com.ytjj.qmyx.mall.model.AdvertReportExample;
import com.ytjj.qmyx.mall.model.Product;
import com.ytjj.quartz.enums.AdvertReportTypeEnums;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class AdvertReportService {
    /**
     * 快手上报url
     */
    private static final String KS_REPORT_URL = "http://ad.partner.gifshow.com/track/activate?";
    /**
     * 头条上报url
     */
    private static final String TT_REPORT_URL = "https://ad.oceanengine.com/track/activate?";


    @Resource
    private AdvertReportMapper advertReportMapper;

    @Resource
    private ProductMapper productMapper;

    /**
     * 快手
     */
    public void sendAdvertReportKS(){
        AdvertReportExample advertReportExample = new AdvertReportExample();
        advertReportExample.createCriteria()
                .andStatusEqualTo(0)
                .andAdvertPlatformEqualTo("KS");
        List<AdvertReport> advertReport = advertReportMapper.selectByExample(advertReportExample);
        advertReport.stream().forEach(item ->{
            String result = null;
            if(AdvertReportTypeEnums.PLACE_ORDER.name().equals(item.getType())){
                // 提交订单发布报告
            }
            if(AdvertReportTypeEnums.APP_ACTIVE.name().equals(item.getType())){
                result=reportKS(item.getCallback(),"1");
            }
            if(AdvertReportTypeEnums.APP_REGISTER.name().equals(item.getType())){
                result=reportKS(item.getCallback(),"2");
            }
            // 修改状态
            if(result!=null){
                JSONObject obj = JSONObject.parseObject(result);
                // 上报成功
                if("1".equals(obj.get("result").toString())){
                    item.setStatus(1);
                }
            }
            advertReportMapper.updateByPrimaryKeySelective(item);
        });
    }

    public void sendAdvertReportTT(){
        AdvertReportExample advertReportExample = new AdvertReportExample();
        advertReportExample.createCriteria()
                .andStatusEqualTo(0)
                .andAdvertPlatformEqualTo("TT");
        List<AdvertReport> advertReport = advertReportMapper.selectByExample(advertReportExample);
        advertReport.stream().forEach(item ->{
            String result=reportTT(item);
            // 修改状态
            if(result!=null){
                JSONObject obj = JSONObject.parseObject(result);
                // 上报成功
                if("0".equals(obj.get("code").toString())){
                    item.setStatus(1);
                }
            }
            advertReportMapper.updateByPrimaryKeySelective(item);
        });
    }

    /**
     * 1 激活
     * 2 注册
     * 14 提交订单
     * @return
     */
    private String reportKS(String eventType,String purchaseAmount,String callback){
        Map<String,String> param = new HashMap<>();
        param.put("event_type",eventType);
        param.put("event_time", String.valueOf(System.currentTimeMillis()));
        param.put("purchase_amount",purchaseAmount);
        param.put("callback",callback);
        String result = HttpClientUtil.doGet(KS_REPORT_URL,param);
        log.debug("快手上报数据返回："+result);
        return result;
    }

    private String reportKS(String callbackurl,String eventType){
        Map<String,String> param = new HashMap<>();
        param.put("event_type",eventType);
        param.put("event_time", String.valueOf(System.currentTimeMillis()));
        String result = HttpClientUtil.doGet(KS_REPORT_URL,param);
        log.debug("快手上报数据返回："+result);
        return result;
    }

    /**
     * 0 激活
     * 1 注册
     * 20 app内下单
     * @param advertReport
     * @return
     */
    private String reportTT(AdvertReport advertReport){
        Map<String,String> param = new HashMap<>();
        if(AdvertReportTypeEnums.APP_ACTIVE.name().equals(advertReport.getType())){
            param.put("event_type","0");
        }else if(AdvertReportTypeEnums.APP_REGISTER.name().equals(advertReport.getType())){
            param.put("event_type","1");
        }else{
            return null;
        }
        if("android".equals(advertReport.getPlatform())){
            param.put("os","0");
            if(StringUtils.isNotBlank(advertReport.getImei())) {
                param.put("imei", Md5Utils.getMD5(advertReport.getImei(),"UTF-8").toLowerCase());
            }else{
                param.put("oaid", advertReport.getOaid());
            }
        }else if("ios".equals(advertReport.getPlatform())){
            param.put("os","1");
            param.put("idfa",advertReport.getIdfa());
        }

        param.put("callback",advertReport.getCallback());
        param.put("conv_time", String.valueOf(System.currentTimeMillis()/1000));
        String result = HttpClientUtil.doGet(TT_REPORT_URL,param);
        log.debug("头条上报数据返回："+result);
        return result;
    }


    public static void main(String[] args) {
        // 快手  test
//        Map<String,String> param = new HashMap<>();
//        param.put("event_type","12");
//        param.put("event_time", String.valueOf(System.currentTimeMillis()));
//        param.put("purchase_amount","990");
//        param.put("callback","dydjRpNweJ84uCO2vG3PgQCmKBNMOWxqZLsWia5_1yGn38mCPqduE7ssYYxZRGafevTFJ0C-A-4-jNFBZPR6ffINOGdSxeEc6uU1uELdIjy9IIsE_o_TxDpJnurgUQMSU9CYj59aTreuBn7tdkr2v_P7FvlkaNxQOeVJTVYPMTnqgYoBJS4XzuR9sFJlRg5J");
//        String result = HttpClientUtil.doGet(url,param);
//        System.out.println(JSON.toJSON(result));
//        System.out.println(System.currentTimeMillis());

        // 腾讯 test
//        try {
//            AddUserActionSets addUserActionSets = new AddUserActionSets();
//            addUserActionSets.init();
//            UserActionSetsAddResponseData response = addUserActionSets.addUserActionSets();
//            System.out.println("腾讯上报返回数据：{}"+JSON.toJSONString(response));
//        } catch (TencentAdsResponseException e) {
//            e.printStackTrace();
//        } catch (TencentAdsSDKException e) {
//            e.printStackTrace();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

}
